#!/usr/bin/python3
# coding: utf-8
# simple_fetcher.py
# 2018-07-10
# TenaciousZ
# 快速创建一个爬虫项目结构
import os, time


auther = 'TenaciousZ'
create_date = time.strftime("%Y-%m-%d")
base_text = '''#!/usr/bin/python3
# coding: utf-8
# {file_name}
# {create_date}
# {auther}
# {desc}'''

add_text = '''\n\n# 加这句话的目的是为了注册临时的环境变量, 让子文件夹的模块可以互相调用\nimport sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))\n'''

requirements_text = '''requests
pymysql
watchdog
pyquery
demjson
fake_useragent
'''
readme_text = '''1.是什么: 就是一个创建爬虫项目目录的文件, 只需要0.3秒

2.怎么使用: 如果你安装了python3, 直接双击文件就可以了
'''

project_name = 'Crawlers'


class SimpleFetcher(object):
	'0.3秒创建一个爬虫项目'
	base_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), project_name)
	paths = []

	@classmethod
	def util_write(cls, path, str=''):
		'创建文件或写入字符串'
		with open(path, 'w', encoding='utf-8') as f:
			f.write(str)

	@classmethod
	def util_create(cls):
		'创建目录或者文件'
		for p in cls.paths:
			path 	= p.get('path', 1)
			is_file = p.get('is_file')

			if not os.path.exists(path):
				if is_file:
					cls.util_write(path, p.get('file_text'))
				else:
					os.makedirs(path)
				print('Create path `%s`' % path)

	@classmethod
	def util_add_path(cls, path, is_file=False, file_name='', has_base_text=False, add_text='',  desc=''):
		'添加路径'
		file_text = ''
		if is_file: 
			path = os.path.join(path, file_name)
			if has_base_text:
				file_text = base_text.format(file_name=file_name, create_date=create_date, auther=auther, desc=desc)
				file_text += add_text
			else:
				file_text = add_text
		p = {
			'path': path, 
			'is_file': is_file, 
			'file_text': file_text
			}
		cls.paths.append(p)

	@classmethod
	def util_callable_add_mehtod(cls):
		'调用add_开头的方法'
		for key in cls.__dict__.keys():
			if key.startswith('add_') and isinstance(cls.__dict__[key], classmethod):
				func = getattr(cls, key)
				if callable(func):
					func()
	# 添加目录, 文件
	@classmethod
	def add_base(cls):
		'根目录下'
		path = cls.base_dir
		cls.util_add_path(path)
		cls.util_add_path(path, is_file=True, has_base_text=True, file_name='setting.py', desc='配置文件')
		cls.util_add_path(path, is_file=True, file_name='README.MD', add_text=readme_text)
		cls.util_add_path(path, is_file=True, file_name='requirements.txt', add_text=requirements_text)

	@classmethod
	def add_spiders(cls):
		'spiders目录'
		path = os.path.join(cls.base_dir, 'spiders')
		cls.util_add_path(path)
		cls.util_add_path(path, is_file=True, has_base_text=True, file_name='__init__.py', desc='')
		cls.util_add_path(path, is_file=True, has_base_text=True, file_name='baidu_api_crawler.py', add_text=add_text, desc='')

	@classmethod
	def add_parsers(cls):
		'parsers目录'
		path = os.path.join(cls.base_dir, 'parsers')
		cls.util_add_path(path)
		cls.util_add_path(path, is_file=True, has_base_text=True, file_name='__init__.py', desc='')
		cls.util_add_path(path, is_file=True, has_base_text=True, file_name='baidu_api_parser.py', add_text=add_text, desc='')

	@classmethod
	def add_etlers(cls):
		'etlers目录'
		path = os.path.join(cls.base_dir, 'etlers')
		cls.util_add_path(path)
		cls.util_add_path(path, is_file=True, has_base_text=True, file_name='__init__.py', desc='')
		cls.util_add_path(path, is_file=True, has_base_text=True, file_name='baidu_api_etler.py', add_text=add_text, desc='')

	@classmethod
	def add_helpers(cls):
		'helpers目录'
		path = os.path.join(cls.base_dir, 'helpers')
		cls.util_add_path(path)
		cls.util_add_path(path, is_file=True, has_base_text=True, file_name='__init__.py', desc='')

	@classmethod
	def add_other(cls):
		'其他目录'
		cls.util_add_path(os.path.join(cls.base_dir, 'tmp'))
		cls.util_add_path(os.path.join(cls.base_dir, 'bak'))
		cls.util_add_path(os.path.join(cls.base_dir, 'logs'))

	@classmethod
	def init(cls):
		cls.util_callable_add_mehtod()
		cls.util_create()
		# print(cls.paths)



if __name__ == '__main__':
	SimpleFetcher.init()
